package boardGenerator;

/**
 * Permite mostrar un tablero de sudoku por la salida
 * estandar, ya sea en forma de tablero o en una linea
 * de texto definida por el numero del cacillero y puntos
 * donde hay espacio en blacno
 */
public class BoardPrint {
     
    /**
    * Imprime el tablero como una grilla
    * @param board 
    */
    public static void print(int[][] board) {
        int size = 3;
        int regionSize = size;
        size = size * size;
        // line to separate the region we build it in a StringBuffer
        StringBuffer buffer = new StringBuffer(size * size * size);
        buffer.append('+');
        for(int i = 0; i < size * 2 + size - 2; i++) {
                buffer.append('-');
        }
        // if I use 2 digits to represent the number
        if(size >= 16) {
                for(int i = 0; i < regionSize * 2 + 4; i++)
                        buffer.append('-');
        }
        buffer.append('+');
        // saved in a String
        String dash = new String(buffer);
        // and we continue with the numbers
        buffer.append("\n|");
        for(int i = 0; i < size; i++) {        // for every row
                for(int j = 0; j < size; j++) {    // and column
                        // depending of the size of the display
                        // we may have to pad with spaces
                        if(size >= 16)
                        {
                                if(board[i][j] < 16)
                                        buffer.append(' ');
                        }

                        buffer.append(' ');
                        if (board[i][j] != 0){
                            buffer.append(Integer.toHexString((board[i][j])).toUpperCase()); // add value to String
                        } else {
                            buffer.append("-");
                        }

                        // add a | to separate the regions
                        if(j % regionSize == regionSize - 1)
                                buffer.append(" | ");
                }
                // add a serie of dash every region
                if(i % regionSize == regionSize -1) {
                        buffer.append('\n').append(dash);
                }
                buffer.append('\n');
                // add a | but if it is the last one
                if(i < size -1)
                        buffer.append('|');
        }
        System.out.println(buffer);
    }
        
    /**
    * Imprime el tablero en una linea continua de numeros
    * @param board 
    */
    public static void printLine(int[][] board){
        /*Linea*/
        System.out.println("Linea");            
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                if (board[i][j]!= 0){
                    System.out.print(board[i][j]);
                }
                else {
                    System.out.print(".");
                }
            }                
        }
        System.out.println("");
    }
}
